package com.yc.ycbook.common

import android.os.Bundle
import com.yc.ycbook.common.ui.MainActivity
import com.yc.ycbook.R
import com.yc.ycbook.base.BaseActivity
import com.yc.yclibx.file.YcImgUtils
import com.yc.yclibx.permissions.YcUtilPermission
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.common_welcome_activity.*
import java.util.concurrent.TimeUnit

/**
 * 欢迎页面用于版本更新、获取权限
 */
class CommonWelcomeActivity : BaseActivity() {
    private val mBgUrl = "https://i0.hdslb.com/bfs/article/7b2ea5247e54d29883d96d80517cb1c39bdd9c20.jpg@1320w_1890h.webp"

    override fun getLayoutId(): Int {
        return R.layout.common_welcome_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        YcImgUtils.loadNetImg(this, mBgUrl, welcomeBgIv)
        getPermission()
    }

    private fun getPermission() {
        YcUtilPermission.newInstance(activity).addPermissions(YcUtilPermission.PERMISSION_STORAGE).setSuccessCall {
            toMainActivity()
        }.setFailCall {
            showToast("获取权限失败，请重启~！")
        }.setNeverAskAgainCall {
            showToast("获取权限失败，请手动在设置里开启~！")
        }.start()
    }

    private fun toMainActivity() {
        addDisposable(Observable.timer(2, TimeUnit.SECONDS).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe {
            MainActivity.newInstance(activity)
        })
    }
}